home *** CD-ROM | disk | FTP | other *** search
/ El Mac 1 / Magazine.iso / EL MAC 1 / Shareware / Comms. & Networking / Batch FTP / 3rd party documentation / Jon’s Commands Read Me
Encoding:
Text File  |  1995-07-07  |  20.4 KB  |  362 lines  |  [TEXT/ttxt]

  1. Jon’s Commands
  2. Copyright 1993-95 by Jon Pugh
  3. (408) 974-0629
  4. jonpugh@netcom.com
  5. http://www.infoworkshop.com/~jonpugh/
  6.  
  7. Free for noncommercial use.
  8. Contact me for a simple cheap license if you wish to include any or all of these as part of a software solution that you sell.
  9.  
  10. This AppleScript scripting addition provides AppleScript and all scriptable and Apple Event aware applications with added functionality.  To examine the AppleScript syntax of each of these commands, simply drag Jon’s Commands onto the Script Editor or use the Open Dictionary command to open it.  To install it, simply drop it into the Scripting Additions folder which is inside your Extensions folder.
  11.  
  12. Jon’s Commands includes these commands:
  13.    deleteFile - delete a file, empty folder or a list of files and/or empty folders
  14.    renameFile - rename a file or folder
  15.    moveFile - move a file, folder or a list of files and/or folders to another folder
  16.    copyFile - copy a file, folder or a list of files and/or folders
  17.    sound volume - get the volume setting
  18.    set sound volume to - set the volume setting
  19.    clipboard info - get a list of data on the clipboard
  20.    set the clipboard to - put data on the clipboard
  21.    the clipboard - get data from the clipboard
  22.    execute FKEY - run an FKEY resource
  23.    screen list - describe the monitor configuration
  24.    finder selection - return the Finder’s selection
  25.    keys pressed - get a list of pressed keys
  26.    machine environment - get info about the machine
  27.    play sound - play sound resources, files and descriptors
  28.    run script resource - run an ‘Scpt’ or ‘scpt’ resource
  29.    free memory - return the free memory available
  30.    the ticks - return the current value of tickCount
  31.    walk folders - walk folders and run a script on each file
  32.    set cursor to - provide feedback with the cursor
  33.    AE user interaction level - control the interaction level
  34.  
  35. Jon’s Commands includes these objects:
  36.    picture - for getting them off the clipboard «class PICT»
  37.    sound - for getting them off the clipboard & playing them «class snd »
  38.    screen info - for describing the monitors
  39.    environment info - for describing the machine
  40.  
  41. Jon’s Commands includes these coercions:
  42.    string to file specification - ‘TEXT’ to ‘fss ’
  43.    styled text to file specification - ‘STXT’ to ‘fss ’
  44.    international text to file specification - ‘itxt’ to ‘fss ’
  45.  
  46. Notes for specific commands:
  47.  
  48. finder selection:
  49.  
  50. This Addition gets the Finder’s selected files and folders. This works with all 7.x Finders (up to and including the scriptable Finder). Using it with any newer Finder could cause it to fail.  Hopefully it won’t crash, but it probably won’t work.  I don’t check for the version because it hasn’t broken yet and people were annoyed by the limitation.
  51.  
  52. You can place script applications in the Apple Menu Items folder or with either Jens Alfke’s ScriptMenu or Leonard Rosenthal’s OSA Menu script menus and use the finder selection function to have your script operate on the selected files.  Way cool.
  53.  
  54. Compared to the Scriptable Finder, which returns a list of object specifiers, this returns a list of aliases (or just one).
  55.  
  56. example:
  57. finder selection
  58.  
  59. copyFile, deleteFile, moveFile & renameFile:
  60.  
  61. These commands work on files and lists of files.  You can use file references (file "disk:folder:foo"), strings of pathnames or aliases to specify the files and folders for these commands. In contrast to the Scriptable Finder, these functions don’t put up dialogs, batch their operations and use temporary memory when needed.
  62.  
  63. RenameFile can only change the name of a file or folder.  The source is a file or folder reference, alias or pathname and the destination is just the new name.  It cannot be a new location.
  64.  
  65. DeleteFile is permanent and immediate.  Be very careful with it.  I recommend using “moveFile foo to (path to trash)” if you are at all worried or inclined toward cautiousness. This can delete empty folders.
  66.  
  67. MoveFile only moves files or folders around on the disk they are on.  It does NOT copy between disks.  It cannot rename or replace files either.  The destination is a folder, which you can specify either with a file reference, an alias or as a pathname string.
  68.  
  69. CopyFile uses temporary memory if available and tries to copy the file in 1 pass for maximum speed. The source may be either a file or a folder, or list of files and/or folders. The destination may be either a file or folder to copy to, unless the source is a folder in which case the destination must be a folder. The source folder will be copied into the destination folder. You cannot replace an existing folder so the replacing parameter is ignored when copying folders. You cannot rename a folder while copying it, so you’ll need to use renameFile after the copy completes.
  70.  
  71. If you try to replace a busy file, the copy will succeed but the busy file will be left in the temporary items folder and will appear in the Rescued Items folder in the trash after the next boot. It will then be safe to delete when you next empty the trash.
  72.  
  73. example:
  74. copyFile "disk:folder:foo" to "disk2:folder:bar" with replacing
  75. copyFile "disk:folder:foo" to "disk2:folder:bar" replacing yes
  76. copyFile "disk:folder:foo" to "disk2:folder:bar" without replacing
  77. copyFile "disk:folder:foo" to "disk2:folder:bar" replacing ask
  78. deleteFile "disk:folder:foo"
  79. deleteFile file "disk:folder:"
  80. moveFile alias "disk:folder:foo" to "disk:folder2:"
  81. renameFile alias "disk:folder:foo" to "bar"
  82. copyFile {"disk:folder:foo", alias "disk2:folder:bar:"} to "disk3:folder:"
  83. deleteFile {file "disk:folder:foo", "disk2:folder:bar"}
  84.  
  85. execute FKEY:
  86.  
  87. This loads the FKEY resource and jumps to it. It does not press command-shift-N, so it will not trigger QuicKeys macros or any function which patches things to run at FKEY-like key combinations.
  88.  
  89. example:
  90. execute FKEY "Switch-a-roo"
  91. execute FKEY 5
  92.  
  93. keys pressed:
  94.  
  95. Keys Pressed returns the GetKey info in the form of a list of key names in string form.
  96.  
  97. example:
  98. keys pressed contains "option"
  99. keys pressed = {"option","command"}
  100.  
  101. machine information:
  102.  
  103. Machine Information returns a keyworded record object of common machine information from Gestalt. You can also feed it a Gestalt selector and it will return the long word response.  This provides complete access to all of the Gestalt functions that exist. You can use the “check bit” parameter to return a boolean which tells if that bit is on.  Bits are numbered from 1 to 32 with number 1 being the lowest or rightmost bit. In this way you can get any information from Gestalt.
  104.  
  105. The environment info record provides these properties:
  106.  
  107. machine type string  -- The name of the Macintosh model.
  108. CPU type string  -- The CPU installed.
  109. System version string  -- The current System version.
  110. FPU boolean  -- Does this machine have an FPU?
  111. Color Quickdraw boolean  -- Does this machine have color Quickdraw?
  112. AppleTalk version extended real  -- The current AppleTalk version.
  113. keyboard string  -- The name of the main keyboard.
  114. Data Access Manager boolean  -- Does this machine have the Data Access Manager installed?
  115. software power off boolean  -- Does this machine turn off when shutdown?
  116. logical RAM integer  -- Amount of RAM available to the system.
  117. physical RAM integer  -- Amount of RAM installed in this machine.
  118. active scripts integer  -- Number of script systems active.
  119. virtual memory boolean  -- Is virtual memory on?
  120. scriptable Finder boolean  -- Is the scriptable Finder present?
  121.  
  122. Both this and keys pressed use STR# resources if you are inclined to localize them.  The machine info is a subset of the available Gestalt selectors.  If you have need of others, let me know and we might be able to include them in a future version, although you can get raw information for any gestalt selector directly.
  123.  
  124. example:
  125. machine information
  126. scriptable Finder of machine information
  127. machine information "code"
  128.  
  129. clipboard info, the clipboard & set the clipboard to:
  130.  
  131. All of these can manage multiple data types.  Styled text is the default data type, with plain text (known as string) as the backup.  International text is also supported, as are a lot of other data formats.  Almost any resource type can be manipulated in AppleScript as an AEDesc in applications. Pictures and sounds are also popular to move around.
  132.  
  133. There is a problem with using the clipboard in multiple programs.  Thus, you can only use these commands in the front application. The simplest thing to do is use “activate” first to get the application to the front and then nab or grab the clipboard.  Some applications don’t seem to check if the scrap has changed while they are running since it only changes when you switch layers in System 7.  In this case you can simply activate an app and then activate the original.  
  134.  
  135. example:
  136. clipboard info
  137. set foo to the clipboard
  138. set foo to the clipboard as "PICT"
  139. set the clipboard to foo
  140.  
  141. sound volume & set sound volume to:
  142.  
  143. Both of these use a number between 0 and 7 for the volume, just like everyone else.
  144.  
  145. example:
  146. set oldVol to sound volume
  147. set sound volume to 5
  148. set sound volume to oldVol
  149.  
  150. play sound:
  151.  
  152. This can play sound (‘snd ’) resources installed in the system or the current open files by either name or id number. It can also play sound resources out of files and out of AppleScript variables (typically accessed via the clipboard). You cannot interrupt a sound in progress in this version and the sounds are played synchronously.  This command is a small improvement on Donald’s sample code from the Language Reference Manual.
  153.  
  154. example:
  155. play sound "I’m sorry Dave..."
  156.  
  157. run script resource:
  158.  
  159. This matches the AppleScript Run Script command but works on resources instead of files.  This allows you to bundle several scripts (typically from other OSA components such as QuicKeys) into a single script file or application. I’ve used it to place the ‘scpt’ resources from QuicKeys scripts into an applet and run them from there.
  160.  
  161. There is a problem with having more than 1 ‘scpt’ resource per file and that is the Script Editor.  It finds the script to display by using Get1IndResource('scpt', 1) which can return the wrong resource if you have several. If you are literate with ResEdit you can make sure the proper script resource is last in the file, but that’s less certain than the other technique I recommend, which is to change the script resource’s type to ‘Scpt’ which is not recognized by the Script Editor.  You need to copy the script resource as hex data and paste it into a new empty ‘Scpt’ resource.  This way allows no conflicts.  Another solution is to use my ScriptServer application (available on Info-Mac & gaea) which allows you to create script resources of any type and id.  This makes it simple to create a script application which combines AppleScript and QuicKeys scripts.  You can even record a QuicKeys script, decompile it with ScriptServer, edit it and recompile it as a piece of a script application.
  162.  
  163. Yet another option is simply to record a QuicKeys script, copy it and paste the text into your AppleScript script with the “run script foo in QuicKeys” command with foo as your text or a variable with text in it.  This works well with simple scripts and doesn’t require this osax.  Unfortunately, the Run Script 1.1 osax has a memory leak when doing this.  Running a compiled script resource is marginally faster too.
  164.  
  165. You can reference script resources by name or number.
  166.  
  167. example:
  168. run script resource "Flail madly"
  169.  
  170. free memory:
  171.  
  172. This returns the current value of the function FreeMem.  It shows the amount of free memory available in the current application’s current heap.  Useful for finding memory leaks.
  173.  
  174. example:
  175. free memory
  176.  
  177. the ticks:
  178.  
  179. This returns the current value of TickCount.  This is useful for timing functions from scripts.  Unfortunately, this opens the resource file for Jon’s Commands every time and thus affects your timing slightly, making it more useful for large timings where the overhead is less instead of small ones. The example is a script object which can record a series of times using the ticks. Include it at the beginning of your script and call it’s methods.
  180.  
  181. example:
  182. script SmartTimer
  183.     property IMrunning : false
  184.     property startTime : 0
  185.     property endTime : 0
  186.     property timeLog : {}
  187.     
  188.     on startTimer()
  189.         if IMrunning then
  190.             error "Timer already running"
  191.         end if
  192.         try
  193.             set startTime to the ticks
  194.             set IMrunning to true
  195.         on error theMsg number -1708
  196.             error "You need Jon’s Commands installed to use this script object."
  197.         end try
  198.     end startTimer
  199.     
  200.     on stopTimer()
  201.         if not IMrunning then
  202.             error "Timer is not running"
  203.         end if
  204.         set endTime to the ticks
  205.         set IMrunning to false
  206.         set elapsedTime to endTime - startTime
  207.         set timeLog to timeLog & {elapsedTime}
  208.         return elapsedTime
  209.     end stopTimer
  210.     
  211.     on averageTime()
  212.         set sum to 0
  213.         set n to number of items of timeLog
  214.         repeat with i in timeLog
  215.             set sum to sum + i
  216.         end repeat
  217.         return sum / n
  218.     end averageTime
  219.     
  220.     on getLog()
  221.         return timeLog
  222.     end getLog
  223.     
  224.     on clearLog()
  225.         set timeLog to {}
  226.     end clearLog
  227. end script
  228.  
  229. walk folders:
  230.  
  231. This makes file processors very simple and fast.  It takes a list of files and folders and a script. It then walks through them all and sends each file to an open handler in the script. Note that as of version 1.3.4, walk folders steps backwards through directories (reverse alphabetical order) so that you can delete files without affecting the subsequent indexing. It is possible to make this be an option if people whine enough.
  232.  
  233. The “only using files of type” parameter allows you to specify one or more file types that will be operated on.  Only files of the types specified will be sent to your script parameter. The types are 4 character strings with the obscure codes you will have to find out about.  Normal choices are things like "APPL", "osax" and "TEXT".
  234.  
  235. In addition, there are 2 boolean parameters, “using files” and “using folders”.  “using files” defaults to true and causes all files to be run through the script.  “using folders” defaults to false and causes all folders to be run through the script.  You can set them to true or false by using “with using folders” and/or “without using files”. 
  236.  
  237. example:
  238. on open (theFiles)
  239.     script foo
  240.         on open (theFile)
  241.             display dialog theFile as string
  242.         end open
  243.     end script
  244.     walk folders theFiles with script foo only using files of type {"TEXT", "osas"}
  245. end open
  246.  
  247. set cursor to:
  248.  
  249. You can use the five standard cursors; arrow cursor, watch cursor, I beam cursor, cross cursor & plus cursor, plus a busy cursor which spins.  You can also specify the name or number of a CURS resource or, using the “with cursor list” parameter, an acur resource.  Subsequent calls with the “busy cursor” will increment this cursor one step.
  250.  
  251. The busy cursor keeps an acur and all the CURS resources in memory between calls.  The “with cursor list” parameter is only necessary on the first call and will reinitialize the cursor if always supplied (i.e. the cursor will not spin).  You can clear this memory by setting the cursor to something other than a busy cursor (i.e. watch cursor or arrow cursor).
  252.  
  253. These are the busy cursors available in Jon’s Commands, use their names or numbers:
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276. This does not support color cursors yet.
  277.  
  278. example:
  279. set cursor to busy cursor with cursor list "Zen Cursor"
  280. repeat
  281.     set cursor to busy cursor
  282. end repeat
  283. set cursor to watch cursor
  284.  
  285. AE user interaction level:
  286.  
  287. This allows you to get and set the AppleEvent manager’s user interaction level.  This way you can allow interaction from remote machines or disallow interaction from local scripts.  In order for this to work, the program which is doing the interaction must explicitly check if interaction is allowed.  Hopefully all scriptable applications do.
  288.  
  289. This returns the current state and then optionally sets it to the new state.
  290.  
  291. The only drawback with this command is that you cannot turn off user interaction in the current application from a script run by OSA Menu.  Since the lowest level is “AE interact with self” and a script running by OSA Menu is sending events to the same application, this is always allowed so they think thy can put up dialogs.  Applications need to support a “never interact” mode explicitly.  Apple’s PhotoFlash does. (shameless plug from one of the authors)
  292.  
  293. example:
  294. set oldLevel to AE user interaction level AE interact with self
  295. AE user interaction level oldLevel
  296.  
  297. Coercions:
  298.    string to file specification
  299.    styled text to file specification
  300.    international text to file specification
  301.  
  302. These coercions allow you to use strings where a program or osax expects a file specification.  Note that the AppleScript class “file” is really an object reference, not simply an FSSpec, although it can be coerced to one.  You can create an FSSpec by using this command: “set x to "disk:folder:file" as «class fss »”. They are particularly easy to implement. With these coercions you don’t need to worry about them though.
  303.  
  304.  
  305. Version History:
  306.  
  307. 1.0b1 - first public release
  308. 1.0b2 - fixed key name in “keys pressed” for PowerBook keyboard.
  309. 1.0b3 - added 7.1.1 to version check for “finder selection”.
  310. 1.0b4 - added “run script resource” command.
  311. 1.0b5 - added “free memory” and “the ticks” commands.
  312. 1.0 - official release.
  313. 1.1b1 - added “copyFile” command. Made “moveFile”, “deleteFile”, “renameFile” & “copyFile” accept lists of files, aliases or pathnames. Added string to file specification coercion.
  314. 1.1b2 - fixed bug in “set clipboard to” which could cause changes to the clipboard to be unnoticed and fixed a really minor bug in “execute FKEY” which passed a dirty PC to the FKEY in 24 bit mode.
  315. 1.1b3 - added “walk folders” command, made “finder selection” return aliases for AS 1.0 compatibility and made “screen list” work without color QuickDraw.
  316. 1.1 - official release
  317. 1.1.1b1 - fixed “machine environment” property name conflict with “machine” by renaming it “machine type” and actually implemented the “cpu type” property. Also fixed the sample script for “walk folders” above.
  318. 1.1.1b2 - fixed “execute fkey” error handling, twiddled clipboard code to remove debugger calls & clean up the code a bit, fixed “finder selection” when items were on desktop of volumes other than the startup volume & made “walk folders” send an alias instead of a file specification for compatibility with AS 1.0.
  319. 1.1.1b3 - fixed error handler in “play sound”.
  320. 1.1.1 - official release
  321. 1.1.2b1 - made string to fsspec coercion more picky so that it doesn’t confuse the “run script” osax.
  322. 1.1.2 - official release
  323. 1.1.3b1 - recompiled with debugger breaks off. Ooops.
  324. 1.1.3 - official release
  325. 1.2b1 - added “set cursor to” command.
  326. 1.2b2 - added “AE user interaction level” function.
  327. 1.2 - official release
  328. 1.3b1 - added “only using files of type” parameter to “walk folders” command.
  329. 1.3 - official release
  330. 1.3.1b1 - made aete unpurgeable to avoid Script Editor dictionary bug.
  331. 1.3.1 - official release
  332. 1.3.2b1 - added “scriptable Finder” to “machine environment” record.
  333. 1.3.2b2 - added additional acur and CURS resources and spruced up the readme.
  334. 1.3.2b3 - made “copyFile” delete the temp file even if FSpExchangeFile fails.  Also fixed memory leak in “keys pressed”.
  335. 1.3.2b4 - honest, this time “copyFile” is fixed.  It was FSpDelete that was failing.
  336. 1.3.2b5 - replaced guts of “copyFile” with MoreFiles code. Now it works with drop boxes. Spruced up the readme some more.
  337. 1.3.2 - official release
  338. 1.3.3b1 - Made clipboard commands error when in the background.
  339. 1.3.3b2 - Fixed “copyFile” problem when replacing a nonexistent file.
  340. 1.3.3 - official release
  341. 1.3.4b1 - Made “walk folders” command step backwards through folders.
  342. 1.3.4b2 - Made “copyFile” fail when copying to a nonexistant directory.
  343. 1.3.4b3 - Made “copyFile” return better errors when files and directories are missing. Zeroed location when not replacing so that the Finder will put the icon in the first empty spot.
  344. 1.3.4b4 - Made “copyFile” copy folders too.
  345. 1.3.4 - official release
  346. 1.3.5b1 - Fixed “copyFile” bug when copying file to file with a different name.
  347. 1.3.5 - official release
  348. 1.3.6b1 - Made replacing parameter on “copyFile” accept yes/no/ask & error when used with directories.
  349. 1.3.6b2 - Fixed “walk folders” bug when walking folders; files were fine.
  350. 1.3.6b3 - Fixed “finder selection” crash when trash is selected.  There are probably still problems with selecting PowerTalk items.
  351. 1.3.6b4 - Added “check bit” parameter to “machine environment” command. 
  352. 1.3.6 - official release
  353.  
  354. Feel free to send money or to write with questions, comments or suggestions.
  355.  
  356. Jon Pugh
  357. One Infinite Loop MS 303-3A
  358. Cupertino, CA 95014
  359. (408) 974-6029
  360. jonpugh@netcom.com
  361. http://www.infoworkshop.com/~jonpugh/
  362.